package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BusinessException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageBean;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    // 分页查询
    @Override
    public PageBean<DishVO> findByPage(DishPageDTO dto) {
        //1. 开启分页
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //2. 执行查询
        List<DishVO> list = dishMapper.findList(dto);
        Page<DishVO> page = (Page<DishVO>) list;
        //3. 返回分页
        return new PageBean<>(page.getTotal(), page.getResult());
    }

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    // 新增菜品
    @Transactional // 开启事务
    @Override
    public void save(DishDTO dto) {
        //1. 保存菜品
        Dish dish = BeanUtil.copyProperties(dto, Dish.class);// 将dto与dish名称相同的属性进行copy
        dishMapper.save(dish);// 主键返回
        //2. 保存口味
        //2-1 取出口味集合
        List<DishFlavor> flavorList = dto.getFlavors();
        //2-2 遍历集合
        if (CollUtil.isNotEmpty(flavorList)) { // 相当于 flavorList!=null && flavorList.size()>0
            flavorList.forEach(flavor -> {
                //2-3 关联菜品id
                flavor.setDishId(dish.getId());
                //2-4 保存口味
                dishFlavorMapper.save(flavor);
            });
        }

    }

    // 回显菜品
    @Override
    public DishVO findById(Long id) {
        //1. 先查菜品和分类名称
        DishVO dishVO = dishMapper.findById(id);
        //2. 再查口味列表
        List<DishFlavor> flavorList = dishFlavorMapper.findByDishId(id);
        //3. 组装返回vo
        dishVO.setFlavors(flavorList);
        return dishVO;
    }

    // 修改菜品
    @Transactional // 开启事务
    @Override
    public void update(DishDTO dto) {
        //1. 修改菜品
        Dish dish = BeanUtil.copyProperties(dto, Dish.class);
        dishMapper.update(dish);
        //2. 删除旧口味
        dishFlavorMapper.deleteByDishId(dish.getId());
        //3. 重新添加新口味
        //3-1 取出口味集合
        List<DishFlavor> flavorList = dto.getFlavors();
        //3-2 遍历集合
        if (CollUtil.isNotEmpty(flavorList)) { // 相当于 flavorList!=null && flavorList.size()>0
            flavorList.forEach(flavor -> {
                //3-3 关联菜品id
                flavor.setDishId(dish.getId());
                //3-4 保存口味
                dishFlavorMapper.save(flavor);
            });
        }
    }

    // 删除菜品
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //1. 遍历ids
        if (CollUtil.isNotEmpty(ids)) {
            ids.forEach(id->{
                //2. 根据id查询菜品信息
                DishVO dishVO = dishMapper.findById(id);
                //3. 判断菜品状态
                if (dishVO.getStatus()== StatusConstant.ENABLE) {
                    throw new BusinessException("上架中菜品无法删除");
                }
                //4. 判断菜品是否与套餐有关联 todo
                //5. 删除菜品
                dishMapper.deleteById(id);
                //6. 删除口味
                dishFlavorMapper.deleteByDishId(id);
            });
        }
    }

    //起售 禁售
    @Override
    public void startOrStop(Dish dish) {
        dishMapper.update(dish);
    }

    // 查询菜品列表
    @Override
    public List<DishVO> findList(DishPageDTO dto) {
        //1. 查询菜品列表
        List<DishVO> list = dishMapper.findList(dto);
        //2. 遍历查询口味
        list.forEach(dishVO -> {
            List<DishFlavor> flavorList = dishFlavorMapper.findByDishId(dishVO.getId());
            dishVO.setFlavors(flavorList);
        });
        //3. 返回
        return list;
    }
}
